package icu.ikuns;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author： Kimariyb
 * @Date：2022/05/22/14:43
 * @Description：
 */
public class QuestionEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String questionId = req.getParameter("questionId");
        String questionClass = req.getParameter("questionClass");
        String questionContent = req.getParameter("questionContent");
        String questionAnswer = req.getParameter("questionAnswer");
        int resultCode = -1;
        if (questionId != null && questionClass != null && questionContent != null && questionAnswer != null) {
            int qc = Integer.parseInt(questionClass);
            Question question = new Question(questionId, qc, questionContent, questionAnswer);
            QuestionBank questionBank = new QuestionBank();
            resultCode = questionBank.addQuestion(question);
        }
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=UTF-8");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>实例</title>");
        out.println("</head>");
        out.println("<body>");
        if (resultCode == 1) {
            out.println("<h1>试题增加成功</h1>");
        }
        out.println("</body>");
        out.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
